//商品详情展示
function showDetail(momentId) {
    $.ajax({
        url : "moment/detail",
        async : true,
        type : "GET",
        data : {
            "momentId" : momentId
        },
        // 成功后开启模态框
        success : queryDetail,
        error : function(res) {
            alert("动态详情获取失败");
        },
        dataType : "json"
    });
}
function queryDetail(res) {
    if(res.code!=0){
        alert(res.msg);
    }
    var model = res.data;
    $("#momentTitle").text(model.momentTitle);
    $("#momentText").text(model.momentText);
    if(model.momentType==1) {   //照片
        $("#div_vedio").hide();
        $("#pictureUrl").attr("src", model.mediaUrl);
        $("#div_picture").show();
    }else if(model.momentType==2){  //视频
        $("#div_picture").hide();
        $("#vedioUrl").attr("src", model.mediaUrl);
        $("#div_vedio").show();
    }
    // 显示模态框
    $('#detailModal').modal('show');
}


//通过
function showPass(momentId) {
    if (confirm("确认审核通过该动态？")) {
        $.ajax({
            type: 'get',
            url: 'moment/pass',
            dataType: 'json',
            data: {
                "momentId": momentId,
                "isPassed": 1
            },
            error: function (request) {
                alert("未知错误，审核失败");
            },
            success: function (res) {
                if (res.code == 0) {
                    alert("动态审核通过成功！");
                    window.location.reload();
                } else {
                    alert(res.msg);
                }
            }
        });
    }
}

//不通过
function showReject(momentId) {
    if (confirm("确认审核不通过该动态？")) {
        $.ajax({
            type: 'get',
            url: 'moment/pass',
            dataType: 'json',
            data: {
                "momentId": momentId,
                "isPassed": 2
            },
            error: function (request) {
                alert("未知错误，审核失败");
            },
            success: function (res) {
                if (res.code == 0) {
                    alert("已将动态审核不通过！");
                    window.location.reload();
                } else {
                    alert(res.msg);
                }
            }
        });
    }
}


//置顶
function showTop(momentId) {
    if (confirm("确认改变该动态置顶状态？")) {
        $.ajax({
            type: 'get',
            url: 'moment/top',
            dataType: 'json',
            data: {
                "momentId": momentId
            },
            error: function (request) {
                alert("未知错误，置顶失败");
            },
            success: function (res) {
                if (res.code == 0) {
                    alert("切换置顶成功！");
                    window.location.reload();
                } else {
                    alert(res.msg);
                }
            }
        });
    }
}


//删除
function showDelete(momentId) {
    if (confirm("确认删除该动态？")) {
        $.ajax({
            type: 'get',
            url: 'moment/delete',
            dataType: 'json',
            data: {
                "momentId": momentId
            },
            error: function (request) {
                alert("未知错误，删除失败");
            },
            success: function (res) {
                if (res.code == 0) {
                    alert("动态删除成功！");
                    window.location.reload();
                } else {
                    alert(res.msg);
                }
            }
        });
    }
}


/**
 * 评论展示
 * @param momentId
 * @param pageIndex
 * @param num
 * @param totalPages
 */
function showComment(momentId, pageIndex, num, totalPages) {
    $.ajax({
        url : "moment/comment",
        async : true,
        type : "GET",
        data : {
            "momentId": momentId,
            "pageIndex": pageIndex,
            "num": num,
            "totalPages": totalPages
        },
        // 成功后显示表格数据
        success : function(res) {
            if (res.code == 0) {
                var page = res.data;
                var list = page.list;
                if(list.length==0){
                    alert("该动态暂无评论");
                    return;
                }

                //填充表格数据
                var tableHtml = "";
                for(var i = 0; i<list.length; i++){
                    var model = list[i];
                    tableHtml += '<tr>' +
                        '<th scope="row">' + model.momentComId + '</th>' +
                        '<td>' + model.wxUserId + '</td>' +
                        '<td>' + model.nickname + '</td>' +
                        '<td>' + model.momentComText + '</td>' +
                        '<td>' + model.momentComTime + '</td>' +
                        '<td>' +
                        '<button type="button" class="btn btn-sm btn-danger" onclick="comDelete('+ model.momentComId +','+ momentId +','+ page.pageIndex +','+ page.num +','+ page.totalPages +')">删除</button>' +
                        '</td>' +
                        '</tr>';
                }
                $("#comment-table").html(tableHtml);

                //分页工具
                var navHtml = "";
                //上一页
                if (page.pageIndex == 1) {
                    navHtml += '<li><a href="#" style="color: #888888;pointer-events: none;"><span aria-hidden="true">上一页</span></a></li>';
                }else{
                    navHtml += '<li><a style="cursor: pointer" aria-label="Previous" onclick="showComment(' + momentId +','+ (page.pageIndex-1) +','+ page.num +','+ page.totalPages + ')"><span aria-hidden="true">上一页</span></a></li>';
                }
                //中间页
                for(var i = 1; i<=page.totalPages; i++){
                    if (page.pageIndex==i) {
                        navHtml += '<li><a href="#" style="font-weight:bold;background: #6faed9;pointer-events: none;">'+ i +'</a></li>';
                    }else {
                        navHtml += '<li><a style="cursor: pointer" onclick="showComment(' + momentId +','+ i +','+ page.num +','+ page.totalPages + ')">'+ i +'</a></li>';
                    }
                }
                //下一页
                if (page.pageIndex == page.totalPages) {
                    navHtml += '<li><a href="#" style="color: #888888;pointer-events: none;"><span aria-hidden="true">下一页</span></a></li>';
                }else{
                    navHtml += '<li><a style="cursor: pointer" aria-label="Next" onclick="showComment('  + momentId +','+ (page.pageIndex+1) +','+ page.num +','+ page.totalPages +  ')"><span aria-hidden="true">下一页</span></a></li>';
                }
                $("#comment-nav").html(navHtml);

                // 显示模态框
                $('#commentModal').modal('show');

            } else {
                alert(res.msg);
            }
        },
        error : function(res) {
            alert("评论获取失败");
        },
        dataType : "json"
    });
}

/**
 * 删除
 * @param momentComId
 */
function comDelete(momentComId, momentId, pageIndex, num, totalPages) {
    if (confirm("确认删除该评论？")) {
        $.ajax({
            type: 'get',
            url: 'moment/comment/delete',
            dataType: 'json',
            data: {
                "momentComId": momentComId
            },
            error: function (request) {
                alert("未知错误，删除失败");
            },
            success: function (res) {
                if (res.code == 0) {
                    alert("删除成功！");
                    showComment(momentId, pageIndex, num, totalPages);
                    return;
                } else {
                    alert(res.msg);
                }
            }
        });
    }
}
